const {resolve} = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const {resources, project, right, host} = require('./server/template/config.json');

module.exports = {
    devtool: 'source-map',
    entry: {
        app: ['babel-polyfill', "./src/apps/index.js"],
        bind: ['babel-polyfill', "./src/apps/index-bind.js"],
        callback: ['babel-polyfill', "./src/apps/index-callback.js"]
    },
    output: {
        path: "./server/dist",
        filename: "bundle-[name].js",
        chunkFilename: "chunk-[id].js"
    },
    externals: {
        jquery: 'window.$',
        $: 'window.$',
        'babel-polyfill': 'window._babelPolyfill',
        QMRoute: 'window.QMRoute'

    },
    resolve: {
        modules: ['', 'node_modules', resolve(__dirname, './src/web_modules')],
        extensions: ['', '.web.js', '.js', '.json']
    },
    module: {
        loaders: [
            {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?cacheDirectory'},
            {test: /\.json$/, loader: 'json-loader'},
            {test: /\.less$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader')},
            {test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader')},
            {test: /\.(png|gif)$/, loader: 'file-loader'}
        ]
    },
    plugins: [
        new webpack.BannerPlugin('This file is created by qm'),
        new ExtractTextPlugin('[name].css', {
            disable: false,
            allChunks: true
        }),
        new webpack.DefinePlugin({
            __DEBUG__: true,
            __DEV__: true
        }),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './index.ejs',
            css: resources['css-src'],
            dll: resources['dll-src'],
            drt: resources['drt-src'],
            $PROJECT: JSON.stringify(project),
            $RIGHT: JSON.stringify(right),
            $HOST: JSON.stringify(host),
            chunks: ['app'],
            env: 'dev'
        }),
        new HtmlWebpackPlugin({
            filename: 'bind.html',
            template: './index.ejs',
            css: resources['css-src'],
            dll: resources['dll-src'],
            drt: resources['drt-src'],
            $PROJECT: JSON.stringify(project),
            $RIGHT: JSON.stringify(right),
            $HOST: JSON.stringify(host),
            chunks: ['bind'],
            env: 'dev'
        }),
        new HtmlWebpackPlugin({
            filename: 'callback.html',
            template: './index.ejs',
            css: resources['css-src'],
            dll: resources['dll-src'],
            drt: resources['drt-src'],
            $PROJECT: JSON.stringify(project),
            $RIGHT: JSON.stringify(right),
            $HOST: JSON.stringify(host),
            chunks: ['callback'],
            env: 'dev'
        }),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['app', 'bind'],
            minChunks: Infinity,
            async: true,
            children: true
        }),
        // new webpack.optimize.CommonsChunkPlugin({
        //     async: true,
        //     children: true,
        //     minChunks: 4,
        // }),
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./dll/vendor-manifest.json')
        })
    ]
};